package com.lsa.skienna.challenge.chap2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Hands implements Runnable{
	
	private static final int HAND_CARD_COUNT = 5; 

	public static void main(String[] args) {
		new Hands().run();
	}
	
	static class TaskReader{
		private BufferedReader reader;
		private Map<String, Rank> code2Rank = new HashMap<String, Rank>();
		private Map<String, Suit> code2Suit = new HashMap<String, Suit>();
		
		TaskReader(InputStream stream) {
			this.reader = new BufferedReader(new InputStreamReader(stream));
			for(Rank card : Rank.values()){
				code2Rank.put(card.getCode(), card);
			}
			
			for(Suit suit : Suit.values()){
				code2Suit.put(suit.getCode(), suit);
			}
		}
		
		Task  readNextTask() throws IOException{
			Task task = null;
			
			String line = reader.readLine();
			if (line == null){
				return null;
			}
			
			Scanner scanner = new Scanner(line);
			try{
				Card[] whiteCards = new Card[HAND_CARD_COUNT];
				Card[] blackCards = new Card[HAND_CARD_COUNT];
				for(int i = 0; i < 10; ++i){
					String cardCode = scanner.next();
					if (cardCode.length() != 2) return null;
					
					Rank rank = code2Rank.get(cardCode.substring(0, 1));
					Suit suit = code2Suit.get(cardCode.substring(1, 2));
					
					if (rank == null || suit == null) return null;
					
					Card[] cards = i < HAND_CARD_COUNT? blackCards : whiteCards;
					cards[i % HAND_CARD_COUNT] = new Card(rank, suit);
				}
				
				task = new Task(new Hand(whiteCards), new Hand(blackCards));
			}
			catch(RuntimeException e){
				return null;
			}
			
			return task;
		}
	}
	
	static class HandEstimator{
		static final String WHITE_WINS = "White wins.";
		static final String BLACK_WINS = "Black wins.";
		static final String TIE = "Tie.";
		
		private static final ScoredRule[] rules = new ScoredRule[] { new OneCardRule(),
				new OnePairRule(),  new TwoPairsRule(),
				new ThreeOfAKindRule(), new StraightRule(), new FlashRule(),
				new FullHouseRule(), new FourOfAKindRule(),
				new StraightFlushRule() };
		
		static String estimate(Task task){
			for (int i = rules.length - 1; i > -1; i--) {
				ScoredRule rule = rules[i];
				String result = applyRule(task, rule);
				if (result != null) return result;
			}
			
			return null;
		}
		
		private static String applyRule(Task task, ScoredRule rule) {
			boolean isWhiteMatch = rule.match(task.getWhiteHand());
			HandScorer whiteHandScorer = rule.getHandScorer();
			
			boolean isBlackMatch = rule.match(task.getBlackHand());
			HandScorer blackHandScorer = rule.getHandScorer();
			
			int whiteScore;
			int blackScore;
			whiteScore = whiteHandScorer.nextScore();
			blackScore = blackHandScorer.nextScore();
			while(whiteScore != -1 || blackScore != -1){
				if (whiteScore > blackScore) {
					return WHITE_WINS;
				} else if (whiteScore < blackScore) {
					return BLACK_WINS;
				}
				whiteScore = whiteHandScorer.nextScore();
				blackScore = blackHandScorer.nextScore();
			}
			
			return isWhiteMatch && isBlackMatch? TIE : null;
		}
		
		static int compare(Card[] whiteCards, Card[] blackCards){
			for (int i = whiteCards.length - 1; i > 0; i--) {
				int whiteScore = whiteCards[i].getRank().getScore();
				int blackScore = blackCards[i].getRank().getScore();
				if (whiteScore > blackScore) {
					return 1;
				} else if (whiteScore < blackScore) {
					return -1;
				}
			}
			return 0;
		}

		static abstract class Rule{
			abstract boolean match(Hand hand);
		}
		
		static abstract class HandScorer{
			abstract int nextScore();
		}
		
		static class NoMatchScorer extends HandScorer{
			static NoMatchScorer INSTANCE = new NoMatchScorer();
			@Override
			int nextScore() {
				return -1;
			}
		}
		
		static class SimpleScorer extends HandScorer{
			
			private Card[] cards;
			private int weight;
			private int index;
			
			public SimpleScorer(Card[] cards, int weight) {
				this.cards = cards;
				this.weight = weight;
				this.index = 0;
			}
			
			public void setWeight(int weight) {
				this.weight = weight;
			}

			@Override
			int nextScore() {
				if (index == cards.length) return -1;
				return weight + cards[index++].getRank().getScore();
			}
		}
		
		static abstract class ScoredRule extends Rule{
			protected boolean isMatch;
			abstract HandScorer getHandScorer();
		}
		
		static class OneCardRule extends ScoredRule{
			static final int WEIGHT = 200;
			private Hand hand;
			@Override
			boolean match(Hand hand) {
				this.hand = hand;
				return true;
			}

			@Override
			HandScorer getHandScorer() {
				Card[] cards = copy(hand.getCards());
				Collections.reverse(Arrays.asList(cards));
				return new SimpleScorer(cards, WEIGHT);
			}
		}
		
		static class OnePairRule extends ScoredRule{
			static final int WEIGHT = 300;
			private static PairRule pairRule = new PairRule();
			private Card[] uniqueCards;
			
			@Override
			boolean match(Hand hand) {
				int i = 0;
				isMatch = false;
				uniqueCards = new Card[4];
				for (; i < HAND_CARD_COUNT - 1 && !isMatch; i++) {
					pairRule.setIndex(i);
					isMatch = pairRule.match(hand);
					uniqueCards[0] = hand.getCards()[i];
				}
				if (!isMatch) return false;
				
				Card[] cards = hand.getCards();
				int k = 1;
				for (int j = 0; j < i - 1; j++) {
					uniqueCards[k++] = cards[j];
				}
				
				for (int j = i + 1; j < HAND_CARD_COUNT; j++) {
					uniqueCards[k++] = cards[j];
				}
				
				return true;
			}

			public Card[] getRemainingCards() {
				return uniqueCards;
			}

			@Override
			HandScorer getHandScorer() {
				return isMatch ? new SimpleScorer(uniqueCards,WEIGHT) : NoMatchScorer.INSTANCE;
			}
		}
		
		static class TwoPairsRule extends ScoredRule{
			static final int WEIGHT = 400;
			private static PairRule pairRule = new PairRule();
			private static int[][] indexPairs = {{0, 2, 4}, {0, 3, 2}, {1, 3, 0}};
			
			private Card[] uniqueCards;

			@Override
			boolean match(Hand hand) {
				uniqueCards = new Card[3];
				for(int[] indeces : indexPairs){
					int threeCardIdx = 2;
					isMatch = true;
					for (int i = 0; i < 2; i++) {
						pairRule.setIndex(indeces[i]);
						if (!pairRule.match(hand)) {
							isMatch = false;
							break;
						}
						uniqueCards[--threeCardIdx] = hand.getCards()[indeces[i]];
					}
					uniqueCards[2] = hand.getCards()[indeces[2]];
					if (isMatch) break;
				}
				return isMatch;
			}

			@Override
			HandScorer getHandScorer() {
				return isMatch ? new SimpleScorer(uniqueCards, WEIGHT) : NoMatchScorer.INSTANCE;
			}
		}
		
		static class ThreeOfAKindRule extends ScoredRule {
			static final int WEIGHT = 500;
			
			private SameValueRule sameValueRule;

			@Override
			boolean match(Hand hand) {
				sameValueRule = new SameValueRule(3, WEIGHT);
				isMatch = sameValueRule.match(hand);
				return isMatch;
			}

			@Override
			HandScorer getHandScorer() {
				return isMatch ? sameValueRule.getHandScorer() : NoMatchScorer.INSTANCE;
			}
		}
		
		static class StraightRule extends ScoredRule{
			static final int WEIGHT = 600;
			private static Rule consecutiveRule = new ConsecutiveRule(HAND_CARD_COUNT);
			private Card highestCard;
			
			@Override
			boolean match(Hand hand) {
				isMatch = consecutiveRule.match(hand);
				if (!isMatch) return false;
				highestCard = hand.getCards()[HAND_CARD_COUNT - 1];
				return true;
			}

			@Override
			HandScorer getHandScorer() {
				return isMatch ? new SimpleScorer(new Card[]{highestCard}, WEIGHT) : NoMatchScorer.INSTANCE;
			}
		}
		
		static class FlashRule extends ScoredRule{
			static final int WEIGHT = 700;
			private static SameSuitRule sameSuitRule = new SameSuitRule();
			private Hand hand;
			
			@Override
			boolean match(Hand hand) {
				isMatch = sameSuitRule.match(hand);
				this.hand = hand;
				return isMatch;
			}

			@Override
			HandScorer getHandScorer() {
				if (isMatch){
					Card[] cards = copy(hand.getCards());
					Collections.reverse(Arrays.asList(cards));
					return new SimpleScorer(cards, WEIGHT);
				}
				
				return NoMatchScorer.INSTANCE;
			}
		}
		
		static class FullHouseRule extends ScoredRule{
			static final int WEIGHT = 800;
			private static SameValueRule sameValueRule;
			
			@Override
			boolean match(Hand hand) {
				sameValueRule = new SameValueRule(3, WEIGHT);
				isMatch = sameValueRule.match(hand);
				if (!isMatch) return false;
				isMatch = sameValueRule.getIndex() == 2 || sameValueRule.getIndex() == HAND_CARD_COUNT - 1;
				if (!isMatch) return false;
				
				int index = sameValueRule.getIndex();
				if (index == HAND_CARD_COUNT - 1) index = 0;
				else index = 3;
				
				PairRule pairRule = new PairRule(index);
				isMatch = pairRule.match(hand);
				return isMatch;
			}

			@Override
			HandScorer getHandScorer() {
				return isMatch? sameValueRule.getHandScorer() : NoMatchScorer.INSTANCE;
			}
		}
		
		static class FourOfAKindRule extends ScoredRule{
			static final int WEIGHT = 900;
			private static SameValueRule sameValueRule;

			@Override
			boolean match(Hand hand) {
				sameValueRule = new SameValueRule(4, WEIGHT);
				isMatch = sameValueRule.match(hand);
				return isMatch;
			}

			@Override
			HandScorer getHandScorer() {
				return isMatch? sameValueRule.getHandScorer() : NoMatchScorer.INSTANCE;
			}
		}
		
		static class StraightFlushRule extends ScoredRule{
			static final int WEIGHT = 1000;
			
			private static Rule consecutiveRule = new ConsecutiveRule(HAND_CARD_COUNT);
			private static Rule sameSuitRule = new SameSuitRule();
			
			private Hand hand;

			@Override
			boolean match(Hand hand) {
				this.hand = hand; 
				
				isMatch = consecutiveRule.match(hand) && sameSuitRule.match(hand);
				return isMatch;
			}

			@Override
			HandScorer getHandScorer() {
				if (isMatch){
					Card[] cards = hand.getCards();
					Card highestCard = cards[cards.length - 1];
					return new SimpleScorer(new Card[]{ highestCard }, WEIGHT);
				}
				
				return NoMatchScorer.INSTANCE;
			}
		}
		
		static class PairRule extends Rule{
			private int index;

			public PairRule(int index) {
				this.index = index;
			}
			
			public PairRule() {
			}
			
			public void setIndex(int index) {
				this.index = index;
			}

			@Override
			boolean match(Hand hand) {
				Card[] cards = hand.getCards();
				Rank firstCardRank = cards[index].getRank();
				boolean isMatch = firstCardRank == cards[index + 1].getRank();
				return isMatch;
			}
		}
		
		static class ConsecutiveRule extends Rule{
			private int count;
			
			public ConsecutiveRule(int count) {
				this.count = count;
			}

			@Override
			boolean match(Hand hand) {
				Card[] cards = hand.getCards();
				int prevScore = cards[0].getRank().getScore();
				int currentCount = 1;
				int maxCount = 0;
				for(int i = 1; i < cards.length; ++i){
					int score = cards[i].getRank().getScore();
					if (score - prevScore != 1) {
						currentCount = 1;
					}
					else{
						++currentCount;
						maxCount = Math.max(currentCount, maxCount);
					}
					
					prevScore = score;
				}
				return maxCount == count;
			}
		}
		
		static class SameValueRule extends ScoredRule{
			private int count;
			private int index;
			private Card sameValueCard;
			private int weight;
			
			public SameValueRule(int count, int weight) {
				this.count = count;
				this.weight = weight;
			}
			
			@Override
			boolean match(Hand hand) {
				index = -1;
				Card[] cards = hand.getCards();
				int sameValueCount = 1;
				Card prevCard = cards[0];
				for (int i = 1; i < cards.length; i++) {
					Card card = cards[i];
					if (card.getRank() == prevCard.getRank()){
						sameValueCount++;
						if (sameValueCount == count){
							index = i;
							sameValueCard = card;
							return true;
						}
					}
					else{
						sameValueCount = 1;
					}
					prevCard = card;
				}
				
				return false;
			}

			public int getIndex() {
				return index;
			}

			@Override
			HandScorer getHandScorer() {
				return new SimpleScorer(new Card[]{sameValueCard}, weight);
			}
		}
		
		static class SameSuitRule extends Rule{
			@Override
			boolean match(Hand hand) {
				Card[] cards = hand.getCards();
				Suit suit = cards[0].getSuit();
				for (int i = 1; i < cards.length; i++) {
					if (cards[i].getSuit() != suit) return false;
				}
				return true;
			}
		}
	}
	
	static class Task{
		private Hand whiteCards;
		private Hand blackCards;
		
		Task(Hand whiteCards, Hand blackCards) {
			this.whiteCards = whiteCards;
			this.blackCards = blackCards;
		}

		Hand getWhiteHand() {
			return whiteCards;
		}

		Hand getBlackHand() {
			return blackCards;
		}
	}
	
	static final Comparator<Card> cardComparator = new Comparator<Card>() {
		@Override
		public int compare(Card o1, Card o2) {
			return o1.getRank().getScore() - o2.getRank().getScore();
		}
	};
	
	static class Hand{
		private Card[] cards;
		
		Hand(Card[] cards) {
			this.cards = cards;
			
			Arrays.sort(this.cards, cardComparator);
		}

		Card[] getCards() {
			return cards;
		}
	}
	
	static Card[] copy(Card[] cardArray){
		Card[] newCardArray = new Card[cardArray.length];
		System.arraycopy(cardArray, 0, newCardArray, 0, cardArray.length);
		return newCardArray;
	}
	
	static class Card{
		private Rank rank;
		private Suit suit;
		
		Card(Rank rank, Suit suit) {
			this.rank = rank;
			this.suit = suit;
		}

		public Suit getSuit() {
			return suit;
		}

		public Rank getRank() {
			return rank;
		}

		@Override
		public String toString() {
			return ""+rank.humanReadable+"_"+suit;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((rank == null) ? 0 : rank.hashCode());
			result = prime * result + ((suit == null) ? 0 : suit.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Card other = (Card) obj;
			if (rank != other.rank)
				return false;
			if (suit != other.suit)
				return false;
			return true;
		}
	}
	
	static enum Suit{
		CLUBS("C"),
		DIAMONDS("D"),
		HEARTS("H"),
		SPADES("S");
		
		private String code;
		private Suit(String code){
			this.code = code;
		}
		
		public String getCode() {
			return code;
		}
	}
	
	static enum Rank{
		TWO(2, "2", "2"),
		THREE(3, "3", "3"),
		FOUR(4, "4", "4"),
		FIVE(5, "5", "5"),
		SIX(6, "6", "6"),
		SEVEN(7, "7", "7"),
		EIGHT(8, "8", "8"),
		NINE(9, "9", "9"),
		TEN(10, "T", "10"),
		JACK(11, "J", "JACK"),
		QUEEN(12, "Q", "QUEEN"),
		KING(13, "K", "KING"),
		ACE(14, "A", "ACE");
		
		int score;
		String code;
		String humanReadable;
		
		private Rank(int score, String code, String humanReadable){
			this.score = score;
			this.code = code;
			this.humanReadable = humanReadable;
		}

		int getScore() {
			return score;
		}

		String getCode() {
			return code;
		}
	}
	
	@Override
	public void run() {
		TaskReader taskReader = new TaskReader(System.in);
		Task task;
		
		try {
			while((task = taskReader.readNextTask()) != null){
				String result = HandEstimator.estimate(task);
				System.out.println(result);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
